package com.meichis.yndms.hybrid;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.json.JSONException;
import org.json.JSONObject;

public class MCSWebViewClient extends WebViewClient {
	private static final String TAG = "MCSWebViewClient";
	private boolean isTONew = false;

	public void setisTONew(boolean value) {
		isTONew = value;
	}

	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		// If dialing phone (tel:5551212)
		if (url.startsWith(WebView.SCHEME_TEL)) {
			try {
				Intent intent = new Intent(Intent.ACTION_DIAL);
				intent.setData(Uri.parse(url));
				view.getContext().startActivity(intent);
			} catch (android.content.ActivityNotFoundException e) {
				Log.e(TAG, "Error dialing " + url + ": " + e.toString());
			}
		}
		// If displaying map (geo:0,0?q=address)
		else if (url.startsWith("geo:")) {
			try {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse(url));
				view.getContext().startActivity(intent);
			} catch (android.content.ActivityNotFoundException e) {
				Log.e(TAG, "Error showing map " + url + ": " + e.toString());
			}
		}
		// If startActivity (startActivity:acticityname)
		else if (url.indexOf("startactivity:") >= 0) {
			try {
				JSONObject jsonpack = new JSONObject(url.substring(url.indexOf(":") + 1));
				Intent intent = new Intent();
				if (HybridInterface.class.isInstance(view.getContext())) {
					HybridInterface hybrid = (HybridInterface) view.getContext();
					intent = hybrid.getActivity().getIntent();
				}
				intent.setClassName(view.getContext(), "com.meichis.ylcrmapp.ui." + jsonpack.getString("ActivityName"));
				intent.putExtra("Params", jsonpack.getString("Params"));
				view.getContext().startActivity(intent);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		// If sending email (mailto:abc@corp.com)
		else if (url.startsWith(WebView.SCHEME_MAILTO)) {
			try {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse(url));
				view.getContext().startActivity(intent);
			} catch (android.content.ActivityNotFoundException e) {
				Log.e(TAG, "Error sending email " + url + ": " + e.toString());
			}
		}

		// If sms:5551212?body=This is the message
		else if (url.startsWith("sms:")) {
			try {
				Intent intent = new Intent(Intent.ACTION_VIEW);

				// Get address
				String address = null;
				int parmIndex = url.indexOf('?');
				if (parmIndex == -1) {
					address = url.substring(4);
				} else {
					address = url.substring(4, parmIndex);

					// If body, then set sms body
					Uri uri = Uri.parse(url);
					String query = uri.getQuery();
					if (query != null) {
						if (query.startsWith("body=")) {
							intent.putExtra("sms_body", query.substring(5));
						}
					}
				}
				intent.setData(Uri.parse("sms:" + address));
				intent.putExtra("address", address);
				intent.setType("vnd.android-dir/mms-sms");
				view.getContext().startActivity(intent);
			} catch (android.content.ActivityNotFoundException e) {
				Log.e(TAG, "Error sending sms " + url + ":" + e.toString());
			}
		}

		// Android Market
		else if (url.startsWith("market:")) {
			try {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse(url));
				view.getContext().startActivity(intent);
			} catch (android.content.ActivityNotFoundException e) {
				Log.e(TAG, "Error loading Google Play Store: " + url, e);
			}

		} else {
			view.loadUrl(url);
		}

		return true;
	}

	@Override
	public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
		
		super.onReceivedError(view, errorCode, description, failingUrl);
	}
}
